var util = require('../../utils/util.js');
var amapFile = require('../../utils/amap-wx.js');
var status = require('../../utils/index.js');
var a = require("../../utils/public");
var wcache = require('../../utils/wcache.js');
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        btnLoading:false,
        needAuth: false,
        login_fixed_image:''
    },
    /**
     * 授权成功回调
     */
    authSuccess: function () {
        wx.reLaunch({
            url: '/lionfish_comshop/pages/index/index',
        })
    },
    bindGetUserInfo: function (t) {
        var that = this;
        if (!this.data.btnLoading) {
          var n = t.detail;
          if (this.setData({ btnLoading: true }), "getUserInfo:ok" === n.errMsg) {
            util.login_prosime(that.data.needPosition).then(function () {
              console.log("授权成功")
              that.setData({ btnLoading: false });
              wx.showToast({
                title: '登录成功',
                icon: 'success',
                duration: 2000
              })
              that.authSuccess();
              app.globalData.changedCommunity = true;
              //检查获取位置权限
              that.data.needPosition && location.getGps();
            }).catch(function () {
                // that.cancel();
              console.log('授权失败');
            })
          } else {
            wx.showToast({
              title: "授权失败，为了完整体验，获取更多优惠活动，需要您的授权。",
              icon: "none"
            });
            // that.cancel();
            that.setData({ btnLoading: false });
          }
        }
      },
    authModal: function () {
        if (this.data.needAuth) {
            this.setData({ showAuthModal: !this.data.showAuthModal });
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;
        app.util.request({
            'url': 'entry/wxapp/index',
            'data': {
                controller: 'index.get_loginFixed_bg'
            },
            dataType: 'json',
            success: function(res) {
                let rdata = res.data;
                if (rdata.code == 0) {
                    that.setData({
                        login_fixed_image: rdata.login_fixed_image
                    })
                }
            }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        let that = this;
        util.check_login_new().then((res) => {
            console.log(res)
            if (res) {
                var community = wx.getStorageSync('community');
                var community_id = community.communityId || '';
                that.setData({
                    needAuth: false,
                    community_id: community_id
                });
            } else {
                that.setData({ needAuth: true });
            }
        })
    },
    cancelLogin:function(e){
        let url = e.currentTarget.dataset.link;
        wx.switchTab({ url })
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})